<?php $editorId = uniqid('editor_');?>
<input type="hidden" name="parse" value="0">
<img style="" id="KC_KINDEDITOR_LOADING_IMG_{$editorId}" src="">
<script type="text/javascript">
    (function(){
        var ajax = {:IS_AJAX?'true':'false'};
        var run = function(K) {
            return K.create('textarea[name="{$field.name}"]', {
                filterMode: true,
                allowFileManager : false,
                syncType:'form',
                zIndex: '99999999',
                themesPath: K.basePath,
                width: '100%',
                height: '500px',
                resizeType:1,
                pasteType : 2,
                urlType : 'absolute',
                fileManagerJson : '{:U('fileManagerJson')}',
                uploadJson : '{:U("file/uploadEditor")}',
                extraFileUploadParams: {
                    session_id : '{:session_id()}'
                    },
                items: ['source', 'removeformat', '|' ,'forecolor', '|', 'bold', 'underline','hr',
                 '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'image', 'link', '|', 'fullscreen'],
                htmlTags:{
                    a : ['href', 'name', 'title'],
                    img : ['src', 'width', 'height', 'border', 'alt', 'title', 'align', '.width', '.height'],
                    'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6,span' : [
                        'align', '.text-align', '.color',
                        '.font-weight', '.text-indent', '.margin-left'
                        ],
                    'hr,br,strong,b,em,i,u,strike' : [],
                    'div':[
                        'align','.border', '.text-align', '.color',
                        '.background-color', '.font-weight', '.background',
                        '.font-style', '.text-decoration', '.vertical-align', '.margin-left',
                    ],
                    },
                //新增截图处理回调
                afterCreate:function(){
                    var doc = this.edit.doc;
                    var cmd = this.edit.cmd;
                    //监听粘贴操作
                     $(doc.body).bind('paste', function(ev)
                    {
                        var $this = $(this);
                        var original = ev.originalEvent;
                        //获取截取的图片粘贴的文件
                        var file = original.clipboardData.items[0].getAsFile();
                        //判断是否获取到图片文件，如果获取到进行下面的操作
                        if(file)
                        {
                            var reader = new FileReader();
                            //读取文件
                            reader.onload = function(evt)
                            {
                                    //读取返回的结果
                                    var result = evt.target.result;
                                    var arr = result.split(",");
                                    var data = arr[1]; // raw base64
                                    var contentType = arr[0].split(";")[0].split(":")[1];
                                    console.log(arr[1]);
                                    html = '<img src="' + result + '" alt="" />';
                                    //传送数据进行处理
                                    $.post('kcadmin/file/uploadScreenImg', {editor:html}, function(data){
                                           cmd.inserthtml(data);
                                    });
                            };
                            reader.readAsDataURL(file);
                        }
                    });
                 },
                //实时更新
                afterChange:function(val){
                    this.sync();
                }
            });
        };

        ajax ? ajax_init_editor() : KindEditor.ready(run);

        //ajax请求下 初始化编辑器
        function ajax_init_editor(){
            console.log("{$editorId}");
            var listenMax=10;listenCount=0;
            var timer=setInterval(function(){
                if(listenCount++>listenMax){
                    clearInterval(timer);
                    return;
                }
                if($('#KC_KINDEDITOR_LOADING_IMG_{$editorId}').height()>0){
                    //加载完毕
                    run(KindEditor);
                    clearInterval(timer);
                }
            },200);
        }
    })();
</script>